package com.atguigu.jxc.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.jxc.domain.R;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.DamageListService;
import com.atguigu.jxc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/damageListGoods")
public class DamageListGoodsController {
    
    @Autowired
    private DamageListService damageListService;

    @Autowired
    private UserService userService;

    @PostMapping("save")
    public R save(
            String damageNumber,
            @RequestParam("damageDate") String damageDate,
            @RequestParam("remarks") String remarks,
            @RequestParam("damageListGoodsStr") String damageListGoodsStr,
            HttpSession session){
        //根据session 获取user
        User user = (User) session.getAttribute("currentUser");;
        Integer userId = user.getUserId();


        //封装实体类数据
        DamageList damageList = new DamageList();
        damageList.setUserId(userId);
        damageList.setDamageDate(damageDate);
        damageList.setRemarks(remarks);

        //解析商品列表
        List<DamageListGoods> damageListGoods = JSONObject.parseArray(damageListGoodsStr,DamageListGoods.class);
        //封装商品列表
        damageList.setDamageListGoods(damageListGoods);

        //获取设置报损单编码
        damageList.setDamageNumber(damageNumber);

        //开始保存报损单
        damageListService.save(damageList);

        return R.ok(100,"保存报损单成功",null);
    }


    @PostMapping("list")
    public Map<String,Object> list(@RequestParam("sTime") String sTime,
                                   @RequestParam("eTime") String eTime,
                                   HttpSession session){
        Map<String,Object> map = new HashMap<>();

        //遍历这个list
        List<DamageList> list = damageListService.getListByDate(sTime, eTime);
        //获取用户名
        User user = (User) session.getAttribute("currentUser");
        String trueName = user.getTrueName();

        //给每一项赋值
        for (DamageList damageList : list) {
            damageList.setTrueName(trueName);
        }

        map.put("rows",list);
        return map;
    }

    @PostMapping("goodsList")
    public Map<String,Object> goodsList(@RequestParam("damageListId") String damageListId){
        Map<String,Object> map = new HashMap<>();
        List<DamageListGoods> listGoods = damageListService.getByListId(Integer.parseInt(damageListId));
        map.put("rows",listGoods);
        return map;
    }

}
